﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Text;

namespace LIS2012.DBUtility
{
    /// <summary>
    /// 公共类
    /// </summary>
    public class PubConstant
    {
        static PubConstant()
        {
            PubConstant.Connection = new SqlConnection(PubConstant.ConnectionString);
            PubConstant.Command = PubConstant.Connection.CreateCommand();
        }
        /// <summary>
        /// 获取数据库连接字符串。
        /// </summary>
        public static string ConnectionString
        {
            get
            {
                object obj = AppDomain.CurrentDomain.GetData("ConnectionString");
                if (obj != null) { return obj.ToString(); }
                else
                {
                    Type tp = System.Reflection.Assembly.GetEntryAssembly().GetType("LIS2012.Properties.Settings", false);
                    if (tp != null) { return tp.GetProperty("ConnectionString").GetValue(Activator.CreateInstance(tp), null).ToString(); }
                    else { return string.Empty; }
                }
            }
        }
        /// <summary>
        /// 表示 SQL Server 数据库的一个打开的连接。
        /// </summary>
        public static SqlConnection Connection;
        /// <summary>
        /// 
        /// </summary>
        public static SqlCommand Command;
        /// <summary>
        /// 打开 SQL Server 数据库连接。
        /// </summary>
        /// <returns></returns>
        public static ConnectionState Open()
        {
            if (PubConstant.Connection.State == ConnectionState.Broken || PubConstant.Connection.State == ConnectionState.Closed)
            {
                if (PubConstant.Connection.State == ConnectionState.Broken)
                {
                    PubConstant.Connection.Close();
                }
                PubConstant.Connection.Open();
            }
            return Connection.State;
        }
        /// <summary>
        /// 关闭 SQL Server 数据库连接。
        /// </summary>
        /// <returns></returns>
        public static ConnectionState Close()
        {
            if (PubConstant.Connection.State != ConnectionState.Closed)
            {
                PubConstant.Connection.Close();
            }
            return PubConstant.Connection.State;
        }
        /// <summary>
        /// 初始化 System.Data.SqlClient.SqlCommand 对象的实例
        /// </summary>
        /// <param name="cmdType"></param>
        /// <param name="cmdText"></param>
        /// <param name="cmdParms"></param>
        public static void PrepareCommand(CommandType cmdType, string cmdText, SqlParameter[] cmdParms)
        {
            PubConstant.Open();
            PubConstant.Command.CommandText = cmdText;
            PubConstant.Command.CommandType = cmdType;
            PubConstant.Command.Parameters.Clear();
            if (cmdParms != null)
            {
                PubConstant.Command.Parameters.AddRange(cmdParms);
            }
        }
    }
}
